package com.shi.yibanjia.controller;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shi.yibanjia.common.Result;
import com.shi.yibanjia.common.UserContext;
import com.shi.yibanjia.entity.dto.OrderQueryDto;
import com.shi.yibanjia.entity.po.Order;
import com.shi.yibanjia.service.OrderService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/app/order")
public class OrderController {


    @Resource
    private OrderService orderService;

    @PostMapping("/calcPrice")
    public Result calcPrice(@RequestBody Order order) {
        Double transPortPrice = orderService.calcTransPortPrice(order);
        Double carryPrice = orderService.calcCarryPrice(order);
        return Result.ok(Map.of("transportPrice", transPortPrice, "carryPrice", carryPrice));
    }

    @GetMapping
    @Cacheable(value = "yibanjia:order", key = "'queryParams:'+#orderQueryDto")
    public Result getOrderPage(OrderQueryDto orderQueryDto) {
        Long userId = UserContext.getUser().getId();
        Page<Order> orderPage = null;
        if (orderQueryDto.getOrderStatus().equals(3)) {
            orderPage = orderService.page(new Page<>(orderQueryDto.getPage(), orderQueryDto.getPageSize()), new LambdaQueryWrapper<Order>()
                    // 订单的状态
                    .in(ObjUtil.isNotEmpty(orderQueryDto.getOrderStatus()), Order::getOrderStatus, 3, 4)
                    // 客户未删除的订单
                    .eq(Order::getStatus, orderQueryDto.getStatus())
                    // 用户id
                    .eq(Order::getUserId, userId)
                    // 订单号模糊查询
                    .like(ObjUtil.isNotEmpty(orderQueryDto.getOrderId()), Order::getId, orderQueryDto.getOrderId())
                    // 下单时间降序排列
                    .orderByDesc(Order::getCreateTime));
        } else if (orderQueryDto.getOrderStatus().equals(4)) {
            orderPage = orderService.page(new Page<>(orderQueryDto.getPage(), orderQueryDto.getPageSize()), new LambdaQueryWrapper<Order>()
                    // 订单的状态
                    .in(ObjUtil.isNotEmpty(orderQueryDto.getOrderStatus()), Order::getOrderStatus, 3)
                    // 客户未删除的订单
                    .eq(Order::getStatus, orderQueryDto.getStatus())
                    // 用户id
                    .eq(Order::getUserId, userId)
                    // 订单号模糊查询
                    .like(ObjUtil.isNotEmpty(orderQueryDto.getOrderId()), Order::getId, orderQueryDto.getOrderId())
                    // 下单时间降序排列
                    .orderByDesc(Order::getCreateTime));
        } else {
            orderPage = orderService.page(new Page<>(orderQueryDto.getPage(), orderQueryDto.getPageSize()), new LambdaQueryWrapper<Order>()
                    // 订单的状态
                    .eq(ObjUtil.isNotEmpty(orderQueryDto.getOrderStatus()), Order::getOrderStatus, orderQueryDto.getOrderStatus())
                    // 客户未删除的订单
                    .eq(Order::getStatus, orderQueryDto.getStatus())
                    // 用户id
                    .eq(Order::getUserId, userId)
                    // 订单号模糊查询
                    .like(ObjUtil.isNotEmpty(orderQueryDto.getOrderId()), Order::getId, orderQueryDto.getOrderId())
                    // 下单时间降序排列
                    .orderByDesc(Order::getCreateTime));
        }
        return Result.ok(orderPage);
    }

    @PostMapping
    @CacheEvict(value = "yibanjia:order", allEntries = true)
    public Result addOrder(@RequestBody Order order) {
        order.setUserId(UserContext.getUser().getId());
        orderService.save(order);
        return Result.ok();
    }

    @PutMapping
    @CacheEvict(value = "yibanjia:order", allEntries = true)
    public Result updateOrderStatus(@RequestBody Order order) {
        orderService.update(new LambdaUpdateWrapper<Order>()
                .set(Order::getOrderStatus, order.getOrderStatus())
                .eq(Order::getId, order.getId()));
        return Result.ok();
    }

    @DeleteMapping("/{id}")
    @CacheEvict(value = "yibanjia:order", allEntries = true)
    public Result removeOrderById(@PathVariable("id") Long id) {
        Long userId = UserContext.getUser().getId();
        orderService.update(new LambdaUpdateWrapper<Order>()
                .set(Order::getStatus, 0)
                .eq(Order::getId, id)
                .eq(Order::getUserId, userId));
        return Result.ok();
    }
}
